package com.lenovo.manufacture;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lenovo.manufacture.databinding.ActivityAutomaticfillingBinding;
import com.lenovo.manufacture.databinding.SublayoutBinding;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class AutomaticFilling extends AppCompatActivity {
    private static final String TAG = "AutomaticFilling";
    private final List<LinePostData> postData=new ArrayList<>();
    private final List<PeopleData.DataBean> peopleData=new ArrayList<>();
    private Button btReturn;
    private MyAdapter adapter;
    private RecyclerView recyclerView;
    private ActivityAutomaticfillingBinding binding;
    private final Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("http://hh1.me:5001/Interface/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_automaticfilling);
        initView();
        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(manager);
        getDataInfo(1);
        getDataInfo(2);
        getDataInfo(3);
        getDataInfo(4);
        setPeopleData();
    }

    private void getDataInfo(int a){
        Service servicePost=retrofit.create(Service.class);
        Call<LinePostData> postDataCall=servicePost.getPost(a);
        postDataCall.enqueue(new Callback<LinePostData>() {
            @Override
            public void onResponse(Call<LinePostData> call, Response<LinePostData> response) {
                postData.add(response.body());
            }

            @Override
            public void onFailure(Call<LinePostData> call, Throwable t) {

            }
        });
        
    }

    private void setPeopleData(){
        Service servicePeople=retrofit.create(Service.class);
        Call<PeopleData> peopleDataCall=servicePeople.isPeople();
        peopleDataCall.enqueue(new Callback<PeopleData>() {
            @Override
            public void onResponse(Call<PeopleData> call, Response<PeopleData> response) {
                Log.d(TAG, "onResponse: "+response.body());
                peopleData.addAll(response.body().getData());
                adapter=new MyAdapter();
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<PeopleData> call, Throwable t) {
                Log.d(TAG, "onFailure: " +t.getMessage());
            }
        });
    }
    private class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{

         class ViewHolder extends RecyclerView.ViewHolder{
            private final SublayoutBinding binding;
            public ViewHolder(@NonNull SublayoutBinding binding) {
                super(binding.getRoot());
                this.binding = binding;
            }
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            SublayoutBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.sublayout, parent, false);
            ViewHolder viewHolder=new ViewHolder(binding);
             setInfo(viewHolder.binding.llJob1,viewHolder,0);
             setInfo(viewHolder.binding.llJob2,viewHolder,1);
             setInfo(viewHolder.binding.llJob3,viewHolder,2);
             setInfo(viewHolder.binding.llJob4,viewHolder,3);
             return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
             setJob(holder.binding.llJob1,"工程师",0,position);
             setJob(holder.binding.llJob2,"技术员",1,position);
             setJob(holder.binding.llJob3,"操作员",2,position);
             setJob(holder.binding.llJob4,"质检员",3,position);
             holder.binding.tvPosition.setText("位置"+(position+1));
        }

        @Override
        public int getItemCount() {
            return postData.size();
        }

        private void setInfo(LinearLayout view,ViewHolder holder,int i){
             view.setOnClickListener(v -> {
                 int adapterPosition =holder.getAdapterPosition()+1;
                 Service service=retrofit.create(Service.class);
                 Call<PositionData> isPosition=service.getPosition(adapterPosition,i);
                 isPosition.enqueue(new Callback<PositionData>() {
                     @Override
                     public void onResponse(Call<PositionData> call, Response<PositionData> response) {
                         Service service1=retrofit.create(Service.class);
                         Call<LinePostData> isPost=service1.getPost(adapterPosition);
                         isPost.enqueue(new Callback<LinePostData>() {
                             @Override
                             public void onResponse(Call<LinePostData> call, Response<LinePostData> response) {
                                 postData.set(adapterPosition -1,response.body());
                                 Toast.makeText(AutomaticFilling.this, "添加", Toast.LENGTH_SHORT).show();
                                 adapter.notifyDataSetChanged();
                             }

                             @Override
                             public void onFailure(Call<LinePostData> call, Throwable t) {

                             }
                         });
                     }

                     @Override
                     public void onFailure(Call<PositionData> call, Throwable t) {

                     }
                 });
             });
        }

        private void setJob(LinearLayout view,String job,int pos,int position){
             TextView nameText=view.findViewById(R.id.name_tv);
             TextView psText=view.findViewById(R.id.strength_tv);
             TextView engineerText=view.findViewById(R.id.tv_engineer);
             LinearLayout job2=view.findViewById(R.id.red_job);
             List<LinePostData.post> jobs=postData.get(position).getData() ;
             int userId=jobs.get(pos).getUserPersonId();
             if (userId!=0){
                 for (PeopleData.DataBean post:peopleData){
                     if (userId==post.getId()){
                         nameText.setText("姓名"+post.getPersonName());
                         psText.setText("体力"+post.getPower());
                         job2.setBackgroundColor(Color.parseColor("#FF00FF"));
                     }
                 }
             }else {
                 nameText.setText("");
                 psText.setText("");
                 job2.setBackgroundColor(Color.parseColor("#FF6A6A"));
             }
             engineerText.setText(job);
        }


    }

    private void initView() {
        recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
        btReturn =findViewById(R.id.bt_return);
        btReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}




